package fr.emn.integration.synthese.presentation;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridLayout;
import java.util.Iterator;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import fr.emn.integration.synthese.abstraction.BDCours;
import fr.emn.integration.synthese.abstraction.Cours;
import fr.emn.integration.synthese.abstraction.Eleve;
import fr.emn.integration.synthese.abstraction.Evaluation;
import fr.emn.integration.synthese.abstraction.Groupe;
import fr.emn.integration.synthese.controle.ControlJButtonSaveEval;
import fr.emn.integration.synthese.controle.ControlJButtonSaveGroupe;
import fr.emn.integration.synthese.controle.EleveDialog.ControlJButtonAddRowEleveDialog;
import fr.emn.integration.synthese.controle.EleveDialog.ControlJButtonDeleteRow;
import fr.emn.integration.synthese.controle.EleveDialog.ControlJButtonValider;
import fr.emn.integration.synthese.controle.EleveDialog.ControlJComboEleveDialog;

public class EleveDialog extends JDialog {

	private static final long serialVersionUID = 1L;

	private BDCours DB;
	private Cours modele;

	private JTextField nom;
	private JTextField prenom;
	private JTextField mail;
	private JComboBox genre;
	private JPanel pane;

	private Object[][] groupes;

	private JComboBox choixGroupe;
	private JComboBox choixModule;


	public EleveDialog (Frame parent, Cours modele,BDCours BD, boolean creation) {
		super(parent, "Eleve", true);

		this.DB=BD;
		this.modele=modele;
		pane=(JPanel) this.getContentPane();
		pane.setLayout(new BorderLayout());
		pane.setMinimumSize(new Dimension(400,500));	

		//ZONE DE SAISIE INFORMATIONS PERSONNELLES ELEVE
		JPanel mainInfo = new JPanel();
		mainInfo.setLayout(new GridLayout(7, 1));
		mainInfo.setMinimumSize(new Dimension(400,500));	
		mainInfo.add(new JLabel("Nom"));
		this.nom = new JTextField();
		mainInfo.add(this.nom);
		mainInfo.add(new JLabel("Prenom"));
		this.prenom = new JTextField();
		mainInfo.add(this.prenom);
		mainInfo.add(new JLabel("Email"));
		this.mail = new JTextField();
		mainInfo.add(this.mail);
		genre = new JComboBox(new String[] {"Homme","Femme"});		
		mainInfo.add(genre);
		pane.add(mainInfo,BorderLayout.NORTH);

		//ATTRIBUTION GROUPE ET MODULE A L'ELEVE
		choixModule = new JComboBox(modele.getModuleNames());
		choixGroupe = new JComboBox(modele.getGroupeNames(modele.findModuleId((String)choixModule.getSelectedItem())));
		ControlJComboEleveDialog controlJCombo = new ControlJComboEleveDialog(choixModule, choixGroupe, modele);
		choixModule.addItemListener(controlJCombo);
		this.groupes = new Object[][]{{choixModule.getItemAt(0), choixGroupe.getItemAt(0), false}};
		String[] titreColonnes = new String[] {"Module","Groupe","Supprimer"}; 
		JTableModulesModel tableModel = new JTableModulesModel(groupes, titreColonnes);
		JTable tableau = new JTable(tableModel);
		tableau.getColumn("Module").setCellEditor(new DefaultCellEditor(choixModule));
		tableau.getColumn("Groupe").setCellEditor(new DefaultCellEditor(choixGroupe));
		JPanel haut = new JPanel();
		haut.setLayout(new BorderLayout());
		haut.add(new JScrollPane(tableau),BorderLayout.CENTER);
		JPanel boutonsPanel = new JPanel();
		JButton addRowButton = new JButton("Ajouter un module");
		ControlJButtonAddRowEleveDialog addRowControl = new ControlJButtonAddRowEleveDialog(parent, modele, tableau);
		addRowButton.addActionListener(addRowControl);
		boutonsPanel.add(addRowButton);
		JButton supprimerMod = new JButton("Supprimer Module(s)");
		boutonsPanel.add(supprimerMod);
		ControlJButtonDeleteRow deleteRow = new ControlJButtonDeleteRow(tableau);
		supprimerMod.addActionListener(deleteRow);
		haut.add(boutonsPanel,BorderLayout.SOUTH);
		pane.add(haut,BorderLayout.CENTER);

		JButton valider = new JButton("Valider");
		ControlJButtonValider valControl = new ControlJButtonValider(this, modele, tableau, creation);
		valider.addActionListener(valControl);
		pane.add(valider,BorderLayout.SOUTH);


		//MISE A JOUR DES DONNEES PERSONNELLES + MODULE/GROUPE DE L'ELEVE
		if (!creation) {
			Eleve amodifier = new Eleve(BD, modele.getEleveCourant());
			nom.setText(amodifier.getNom());
			prenom.setText(amodifier.getPrenom());
			mail.setText(amodifier.getMail());
			genre.setSelectedItem(amodifier.getGenre());
			Iterator it = amodifier.getModules().iterator();
			((JTableModulesModel) tableau.getModel()).removeRow(0);
			int temp =-1;
			while(it.hasNext()){
				temp= (Integer) it.next();
				Object[] donnee = new Object[]{modele.findModule(temp).getNom(), amodifier.getGroupe(temp), false};
				((JTableModulesModel) tableau.getModel()).addRow(donnee);
			}
		}
		this.pack();
		super.setLocationRelativeTo(parent);
	}

	public JTextField getNom() {
		return nom;
	}

	public JTextField getPrenom() {
		return prenom;
	}

	public JTextField getMail() {
		return mail;
	}

	public JComboBox getGenre() {
		return genre;
	}

	public BDCours getDB() {
		return DB;
	}
}
